//
//  SLStatusViewModel.swift
//  Sina15
//
//  Created by 盼 on 16/4/13.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit


class SLStatusViewModel: NSObject {

    //模型属性
    var status: SLStatus?
    
    //用户头像地址
    var iconURL: NSURL?
    
    //微博等级图标
    var mbImage: UIImage?
    
    //微博认证图标
    var verifiedImage: UIImage?
    
    //微博时间
    var created_at: String? {
    
          return NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
    }
    
    //微博来源
    var source: String?
    
    //转发数量
    var forwardTitle: String?
    
    //评论数量
    var commentTitle: String?
    
    //点赞数量
    var favorTitle: String?
    
    //重载构造方法，让模型创建的时候就有status对象
    init(status: SLStatus) {
        
        super.init()
        
        self.status = status
        
        //用户头像地址
        iconURL = NSURL(string: (self.status?.user?.profile_image_url)!)
        
        //等级图标
        dealmbRank()
        
        //设置微博认证类型
        dealVerifiedType()
        
        //微博来源
        dealSource()
        
        //微博时间
//        dealSinatime()
        
        //底部文字处理
        self.forwardTitle = dealToolBarText(status.reposts_count,title: "转发")
        self.commentTitle = dealToolBarText(status.comments_count, title: "评论")
        self.favorTitle = dealToolBarText(status.attitudes_count, title: "赞")
    }
    
}

extension SLStatusViewModel {
    
    //处理微博等级的封装
    func dealmbRank() {
        let mbType = self.status?.user?.mbrank ?? 0
        
        if mbType > 0 && mbType < 7 {
            
            mbImage = UIImage(named: "common_icon_membership_level\(mbType)")
        }
    }
    
    //处理认证类型
    func dealVerifiedType() {
        let verifiedType = self.status?.user?.verified_type ?? 0
        
        switch verifiedType {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named:"avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
    }
    
    //微博来源处理
    func dealSource(){
        //            <a href=\"http://app.weibo.com/t/feed/5iusLt\" rel=\"nofollow\">语录达人</a>
        let startFlag = "\">"
        let endFlag = "</"
        let str = status?.source ?? ""
        if let range1 = str.rangeOfString(startFlag), range2 = str.rangeOfString(endFlag) {
            
            let range = range1.endIndex..<range2.startIndex
             source = "来自" + str.substringWithRange(range)
            
            print(source)
        }
    }
    
//    //微博创建时间
//    func dealSinatime() {
//    
//        //指定格式
//        dateDF.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
//        
//        //指定地区，否则在真机测试上会崩溃
//        dateDF.locale = NSLocale(localeIdentifier: "en")
//        
//        //把对应字符串转换成日期
//        var sinaDate: NSDate?
//        if let str = status?.created_at {
//            sinaDate = dateDF.dateFromString(str)
//        }
//        
//        //把日期对象转换成指定格式的字符串
//        dateDF.dateFormat = "yyyy-MM-dd HH:mm:ss"
//        
//        created_at = dateDF.stringFromDate(sinaDate!)
//        
//        print(created_at)
//    }
    
    //底部文字处理
    func dealToolBarText(count: Int,title: String)->String{
        
        if count <= 0 {
            
            return title
        }else{
            
            if count > 10000 {
                
                return "\(Double(count/1000)/10)万"
            }
            
            return "\(count)"
        }
    }
}
